package com.zunipe.accounts;

import android.app.Application;
import android.content.SharedPreferences;

import com.zunipe.accounts.database.dao.AccountDao;
import com.zunipe.accounts.database.dao.UserDao;
import com.zunipe.accounts.database.entity.User;

import java.util.HashMap;
import java.util.Map;

public class AccountApplication extends Application {
    private static AccountApplication instance;
    private UserDao userDao;
    private AccountDao accountDao;
    private String username;
    private boolean isLogin;
    private int userId;
    private Map<String, Integer> expensesTypeMap;
    private Map<String, Integer> incomeTypeMap;

    public static AccountApplication getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        userDao = new UserDao(this);
        accountDao = new AccountDao(this);
        expensesTypeMap = new HashMap<>();
        incomeTypeMap = new HashMap<>();
        expensesTypeMap.put("餐饮", 0);
        expensesTypeMap.put("购物", 1);
        expensesTypeMap.put("日用", 2);
        expensesTypeMap.put("交通", 3);
        expensesTypeMap.put("蔬菜", 4);
        expensesTypeMap.put("水果", 5);
        expensesTypeMap.put("零食", 6);
        expensesTypeMap.put("娱乐", 7);
        incomeTypeMap.put("工资", 0);
        incomeTypeMap.put("兼职", 1);
        incomeTypeMap.put("理财", 2);
        incomeTypeMap.put("礼金", 3);
        incomeTypeMap.put("其他", 4);
        SharedPreferences userinfo = getSharedPreferences("userinfo", MODE_PRIVATE);
        if (!userinfo.contains("username")) {
            username = "tourist";
            isLogin = false;
            userId = 0;
        } else {
            userId = userinfo.getInt("userid", 0);
            username = userinfo.getString("username", "tourist");
            isLogin = true;
        }
        instance = this;
    }

    public void logout() {
        SharedPreferences userinfo = getSharedPreferences("userinfo", MODE_PRIVATE);
        SharedPreferences.Editor edit = userinfo.edit();
        edit.remove("username");
        edit.remove("userid");
        this.username = "";
        this.userId = 0;
        this.isLogin = false;
        edit.apply();
    }

    public int getIncomeTypeIndex(String type) {
        if (incomeTypeMap != null) {
            if (incomeTypeMap.containsKey(type)) {
                return incomeTypeMap.get(type);
            }
        }
        return 0;
    }

    public int getExpensesTypeIndex(String type) {
        if (expensesTypeMap != null) {
            if (expensesTypeMap.containsKey(type)) {
                return expensesTypeMap.get(type);
            }
        }
        return 0;
    }

    public void setUsername(User user) {
        SharedPreferences userinfo = getSharedPreferences("userinfo", MODE_PRIVATE);
        SharedPreferences.Editor edit = userinfo.edit();
        edit.putString("username", user.getUsername());
        edit.putInt("userid", user.getId());
        this.username = user.getUsername();
        this.userId = user.getId();
        this.isLogin = true;
        edit.apply();
    }

    public int getUserId() {
        return this.userId;
    }

    public boolean isLogin() {
        return this.isLogin;
    }

    public String getUsername() {
        return this.username;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public AccountDao getAccountDao() {
        return accountDao;
    }
}
